package com.framework.web.sys.vo;

import com.framework.core.util.MyBeanUtils;
import com.framework.web.sys.domain.Resource;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fusf on 2016/1/10.
 */
public class ResourceVo extends Resource {

    private String state = "open";// open,closed
    private boolean checked = false;
    private Object attributes;
    private List<ResourceVo> children = new ArrayList<ResourceVo>();

    /**
     * 树子节点递归遍历
     *
     * @param list
     * @return
     */
    public static List<ResourceVo> getRoot(List<ResourceVo> list) throws Exception {
        List<ResourceVo> treeList = new ArrayList<ResourceVo>();
        for (ResourceVo resourceVo : list) {
            if (StringUtils.isEmpty(resourceVo.getParentId())) {
                initChild(resourceVo, list);
                treeList.add(resourceVo);
            }
        }
        return treeList;
    }

    /**
     * 节点遍历
     *
     * @param resourceVo
     * @param list
     */
    private static void initChild(ResourceVo resourceVo, List<ResourceVo> list) {
        for (ResourceVo node : list) {
            if (resourceVo.getId().equals(node.getParentId())) {
                initChild(node, list);
                resourceVo.getChildren().add(node);
                resourceVo.setState("closed");
            }
        }
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public Object getAttributes() {
        return attributes;
    }

    public void setAttributes(Object attributes) {
        this.attributes = attributes;
    }

    public List<ResourceVo> getChildren() {
        return children;
    }

    public void setChildren(List<ResourceVo> children) {
        this.children = children;
    }
}
